Best way to silence the default logger with WTestEnvironment
Added by Marco M over 10 years ago
Hi,
I am using WTestEnvironment and WApplication to UT my code and I don't want to see any Info log from Wt.
By looking at the code and the documentation, it seems there is no way to get hold of the default logger of WApplication, so I cannot call WLogger.configure() to silent it, so it seems that I have to pass a custom wt_config.xml, and I do this by passing the file to the WTestEnvironment constructor.
Now, since I use google test and test fixtures, I don't want to repeat the boilerplate of putting on the stack a WTestEnvironment on each test, I put it in the class fixture.
But, WTextEnvironment is non copiable, so I have to use a pointer. When I delete it in the fixture teardown, clang warns me:
test-urlrouter.cpp:23:9: warning: delete called on 'Wt::Test::WTestEnvironment' that has virtual functions but non-virtual destructor
To summarize :-)
1. it would be nice to avoid configurations file at all when doing UT :-)
- to silent the default logger, is there any other way besides what I am doing?
- can the destructor of WTestEnvironment made virtual or should I really keep it on the stack?
thanks (and btw the code I am writing I will make it available, you might like it I think :-)
marco.m
Replies (5)
RE: Best way to silence the default logger with WTestEnvironment - Added by Koen Deforche over 10 years ago
Hey,
To access the logger you can use WServer::logger(). You can get the server object using WEnvironment::server().
It seems we have not documented WServer::logger(), but we will make it a proper public API.
I've fixed the destructor of WEnvironment to be virtual (oops!).
I'm curious now about your cliffhanger of code to appear that I will like ? :-)
Regards,
koen
RE: Best way to silence the default logger with WTestEnvironment - Added by Marco M over 10 years ago
Thanks Koen, I saw the logger in WServer but I missed that I could get it from WTestEnvironment. It will allow me to get rid of the configuration file :-)
For the cliffhanger :-), what would you think of a Wt version of the Django "URLconf" URL router?
See https://docs.djangoproject.com/en/dev/topics/http/urls
marco.m
RE: Best way to silence the default logger with WTestEnvironment - Added by Marco M over 10 years ago
Hey Koen,
I tried using the logger out of WServer as you suggested, but it is too late:
_env = new Wt::Test::WTestEnvironment();
Wt::WLogger& logger = _env->server()->logger();
logger.configure("* -info -debug");
still generates this output:
[2014-Mar-30 22:58:05.103593] 2496 - [info] "config: reading Wt config file: /etc/wt_config.xml (location = '')"
[2014-Mar-30 22:58:05.109003] 2496 - [info] "Wt: session created (#sessions = 1)"
I think it is because that output is generated by the constructor of WTestEnvironment, before the call to logger.configure().
So, to summarize, the only way to completely silent the logging inside a UT seems to be: pass a custom wt_config.xml to the WTestEnvironment constructor.
RE: Best way to silence the default logger with WTestEnvironment - Added by Koen Deforche over 10 years ago
Hey Marco,
Indeed I hadn't thought of that. Can you file a feature request for this as I agree there should be a better solution for this.
Regards,
koen
RE: Best way to silence the default logger with WTestEnvironment - Added by Marco M over 10 years ago
Hey Koen,
done. See http://redmine.webtoolkit.eu/issues/2893